;; supercollider/server-connect.scm - (c) rohan drape, 2003-2005

;; The <server_> transport mechanism.

(define (server-osc)
  (server_-osc (current-server)))

(define (set-server-osc! value)
  (set-server_-osc! (current-server) value))

;; Connection state.

(define (server-connected?)
  (if (server-osc) #t #f))

;; The connection implementation is in terms of the OSC transport
;; which is over either TCP or UDP.

(defineK (server-connect! (use-tcp? #f))
  (if (server-connected?)
      (error "server-connect: server connected" (server-host-name))
      (let ((p (make-osc-transport 
		(server-network-address)
		'mechanism: (if use-tcp? 'tcp 'udp))))
	(if use-tcp?
	    (osc-send-u8vector 
	     p (string->u8vector (server-option 'session-password))))
	(set-server-osc! p))))

;; Close the connection to `server'.  If there is no connection
;; nothing is done.

(define (server-disconnect!)
  (if (server-connected?)
      (begin (osc-transport-stop (server-osc))
	     (set-server-osc! #f))))
